package com.matrix.java.chap_05;

import java.util.Scanner;

/*
    显示日历
 */
public class Ex5_29 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("年: ");
        int year = input.nextInt();
        System.out.print("星期: ");
        int firstDayOfYear = input.nextInt();
        input.close();

        boolean isLeapYear = false;
        String month;
        String blank = "";
        int monthDays;
        int weekday = firstDayOfYear;
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
            isLeapYear = true;
        }
        for (int i = 1; i <= 12; i++) {
            switch (i) {
                case 1:
                    month = "January";
                    monthDays = 31;
                    break;
                case 2:
                    month = "February";
                    monthDays = 28;
                    weekday += 3;
                    if (isLeapYear) {
                        monthDays = 29;
                    }
                    break;
                case 3:
                    month = "March";
                    monthDays = 31;
                    if (isLeapYear) {
                        weekday += 1;
                    }
                    break;
                case 4:
                    month = "April";
                    monthDays = 30;
                    weekday += 3;
                    break;
                case 5:
                    month = "May";
                    monthDays = 31;
                    weekday += 2;
                    break;
                case 6:
                    month = "June";
                    monthDays = 30;
                    weekday += 3;
                    break;
                case 7:
                    month = "July";
                    monthDays = 31;
                    weekday += 2;
                    break;
                case 8:
                    month = "August";
                    monthDays = 31;
                    weekday += 3;
                    break;
                case 9:
                    month = "September";
                    monthDays = 30;
                    weekday += 3;
                    break;
                case 10:
                    month = "October";
                    monthDays = 31;
                    weekday += 2;
                    break;
                case 11:
                    month = "November";
                    monthDays = 30;
                    weekday += 3;
                    break;
                case 12:
                    month = "December";
                    monthDays = 31;
                    weekday += 2;
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + i);
            }
            if (weekday > 7) {
                weekday -= 7;
            }
            System.out.println("       " + month + " " + year);
            System.out.println("---------------------------");
            System.out.println("Sun Mon Tue Wed Thu Fri Sat");
            switch (weekday) {
                case 7:
                    blank = "";
                    break;
                case 1:
                    blank = "    ";
                    break;
                case 2:
                    blank = "        ";
                    break;
                case 3:
                    blank = "            ";
                    break;
                case 4:
                    blank = "                ";
                    break;
                case 5:
                    blank = "                    ";
                    break;
                case 6:
                    blank = "                        ";
                    break;
            }
            int count = weekday;
            for (int j = 1; j <= monthDays; j++) {
                if (j == 1) {
                    System.out.print(blank);
                }
                count++;
                if (count % 7 == 0 || j == monthDays) {
                    System.out.println(j);
                } else {
                    System.out.print(j + "    ");
                }
            }
        }
    }
}
